import request from '@/utils/request'

// 获取文章新闻推荐
export const getArticleAPI = (channelId, timestamp) => {
  return request({
    url: '/v1_0/articles',
    params: {
      channel_id: channelId,
      timestamp
    }
  })
}

// 获取文章详情
export const getArticleDetailAPI = id => {
  return request({
    url: `/v1_0/articles/${id}`
  })
}

// 获取文章评论
export const getCommentAPI = (id, offset) => {
  return request({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: id,
      offset
    }
  })
}

// 点赞 & 取消点赞
export const LikeArticleAPI = (id, islike) => {
  if (islike) {
    // 点赞
    return request({
      url: '/v1_0/article/likings',
      method: 'POST',
      data: {
        target: id
      }
    })
  } else {
    // 取消点赞
    return request({
      url: `/v1_0/article/likings/${id}`,
      method: 'DELETE'
    })
  }
}

// 收藏文章&取消收藏
export const collectArticleAPI = (id, isCollect) => {
  if (isCollect) {
    // 收藏
    return request({
      url: '/v1_0/article/collections',
      method: 'POST',
      data: {
        target: id
      }
    })
  } else {
    // 取消收藏
    return request({
      url: `/v1_0/article/collections/${id}`,
      method: 'DELETE'
    })
  }
}

// 获取评论回复
export const getReplysByCommentAPI = (commentId, offset) => {
  return request({
    url: '/v1_0/comments',
    params: {
      type: 'c',
      source: commentId,
      offset
    }
  })
}

// 对文章进行评论，对评论进行回复
export const getArticleCommentAPI = (target, content, articleId = null) => {
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    data: { target, content, art_id: articleId }
  })
}
